package thread;
//当使用多个synchronized锁定多个不同的代码片段
public class SyncDemo5 {
    public static void main(String[] args) {
        Coo coo = new Coo();
        Thread t1 = new Thread(coo::methodA);
        Thread t2 = new Thread(coo::methodB);
        t1.start();
        t2.start();
    }
}
class Coo{
    public synchronized void methodA(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":正在执行a方法");
            Thread.sleep(3000);
            System.out.println(t.getName()+":a方法执行完毕");
        } catch (InterruptedException e) {
        }
    }
    public void methodB(){
        synchronized(this){
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName()+":正在执行b方法");
                Thread.sleep(3000);
                System.out.println(t.getName()+":b方法执行完毕");
            } catch (InterruptedException e) {
            }
        }
    }
}